package cn.com.support.utils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.codehaus.jackson.JsonEncoding;
import org.codehaus.jackson.JsonGenerator;
import org.codehaus.jackson.map.ObjectMapper;

import cn.com.commons.utils.StringUtil;
import cn.com.dal.dataobject.sys.VerifyConfigDO;


/**
 * JACKSON 操作JSON工具类
	* @report
	* @Copyright ? 2012 Zoe All Rights Reserved.
	* @Title JacksonUtil.java
	* @Since 2012-3-24
	* @Author Zoe
	* @Version 1.0.0 2012-3-24
 */
public class JacksonUtil {
	
	//定义JACKSON操作对象
	private static JsonGenerator jsonGenerator = null;
    private static ObjectMapper objectMapper = null;
    
    /**
     * 1.完成JSON字符串to JAVABEAN的转换：
     *   notice:json字符串中key必须是bean中的属性
    	* @Title: json2Bean 
    	* @param jsonStr
    	* @param clazz
    	* @return  Object
    	* @Author Zoe 
    	* @Version 1.0.0 2012-3-24
     */
    public static Object json2Bean(String jsonStr, Class<?> clazz){
    	objectMapper = new ObjectMapper();
    	Object obj = null;
    	try {
    		jsonGenerator = objectMapper.getJsonFactory().createJsonGenerator(System.out, JsonEncoding.UTF8);
    		obj = objectMapper.readValue(jsonStr, clazz);
		} catch (Exception e) {
			System.out.println(e);
		}
        return obj;
    }
    
    /**
     * 转换数组TO MAP
    	* @Title: array2Map 
    	* @param objArr
    	* @return Map<String,Object>
    	* @Author Zoe 
    	* @Version 1.0.0 2012-3-29
     */
    public static Map<String, Object> array2Map(Object[] objArr){
    	Map<String, Object> map = new HashMap<String, Object>();
    	try {
			for(Object obj : objArr){
				map.put(obj+"", obj);
			}
		} catch (Exception e) {
			System.out.println(e);
		}
    	return map;
    }
    
    /**
     * 转换JSONARR字符串TO LIST<MAP>
    	* @Title: jsonArr2Lst 
    	* @param jsonArrStr
    	* @return List<Map<String,Object>>    返回类型
    	* @Author Zoe 
    	* @Version 1.0.0 2012-4-5
     */
    @SuppressWarnings("unchecked")
	public static List<Map<String, Object>> jsonArr2Lst(String jsonArrStr){
    	objectMapper = new ObjectMapper();
    	List<Map<String, Object>> res = null;
    	try {
    		if(StringUtil.isNotBlank(jsonArrStr)){
    			res = objectMapper.readValue(jsonArrStr.replaceAll("'", "\""), List.class);
    		}
		} catch (Exception e) {
			System.out.println(e);
		}
    	return res;
    }
    
	@SuppressWarnings("unchecked")
	public static void parseVerifyConfig(VerifyConfigDO configDO){
    	objectMapper = new ObjectMapper();
    	List<Map<String, Object>> res = null;
    	try {
    		if(StringUtil.isNotBlank(configDO.getRoleId())){
    			res = objectMapper.readValue(configDO.getRoleId().replaceAll("'", "\""), List.class);
    		}
    		configDO.setRoleLst(res);
		} catch (Exception e) {
			System.out.println(e);
		}
    }
	
	public static Map json2Map(String jsonStr){
    	objectMapper = new ObjectMapper();
    	Map map = null;
    	try {
    		map = objectMapper.readValue(jsonStr, Map.class);
		} catch (Exception e) {
			System.out.println(e);
		}
        return map;
    }
}
